package com.campus.activity.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.campus.activity.entity.User;
import com.campus.activity.mapper.UserMapper;
import com.campus.activity.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.time.LocalDateTime;
import java.util.List;

/**
 * 用户服务实现类
 * 
 * @author System
 * @since 2024
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {

    @Autowired
    private UserMapper userMapper;
    
    private final BCryptPasswordEncoder passwordEncoder = new BCryptPasswordEncoder();

    @Override
    public User login(String username, String password) {
        User user = getUserByUsername(username);
        if (user != null && verifyPassword(password, user.getPassword())) {
            return user;
        }
        return null;
    }

    @Override
    public boolean register(User user) {
        // 检查用户名是否已存在
        if (isUsernameExists(user.getUsername())) {
            return false;
        }
        
        // 加密密码
        user.setPassword(encodePassword(user.getPassword()));
        user.setCreateTime(LocalDateTime.now());
        user.setUpdateTime(LocalDateTime.now());
        user.setDeleted(0);
        
        return save(user);
    }

    @Override
    public User getUserByUsername(String username) {
        return userMapper.selectByUsername(username);
    }

    @Override
    public List<User> getUsersByRole(String userRole) {
        return userMapper.selectByUserRole(userRole);
    }

    @Override
    public boolean updatePassword(Integer userId, String oldPassword, String newPassword) {
        User user = getById(userId);
        if (user == null || !verifyPassword(oldPassword, user.getPassword())) {
            return false;
        }
        
        User updateUser = new User();
        updateUser.setUserId(userId);
        updateUser.setPassword(encodePassword(newPassword));
        updateUser.setUpdateTime(LocalDateTime.now());
        
        return updateById(updateUser);
    }

    @Override
    public User getUserByStudentId(Integer studentId) {
        return userMapper.selectByStudentId(studentId);
    }

    @Override
    public User getUserByOrganizerId(Integer organizerId) {
        return userMapper.selectByOrganizerId(organizerId);
    }

    @Override
    public boolean isUsernameExists(String username) {
        return getUserByUsername(username) != null;
    }

    @Override
    public boolean verifyPassword(String rawPassword, String encodedPassword) {
        return passwordEncoder.matches(rawPassword, encodedPassword);
    }

    @Override
    public String encodePassword(String password) {
        return passwordEncoder.encode(password);
    }
}